﻿function showStatusList(obj) {
	obj = defaultx(obj, 'page');
	if (!Ext.isNumber(obj.page)) obj.page = 1;
	if (refreshTab('showStatusList', obj)) return;
	var pageSize = 25;

	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : url.getStatusList
						}),
				reader : new Ext.data.JsonReader({
							root : 'result',
							totalProperty : 'total',
							id : 'rid'
						}, ['rid', 'nickname', 'username', 'pid', 'problemTitle', 'status',
								'statusCode', 'lang', 'codeLen', 'allowCode', 'exeTime', 'exeMem',
								'submitTime', 'ceInfo', 'reInfo', 'reCode'])
			});

	var cm = new Ext.grid.ColumnModel([{
				header : 'RID',
				sortable : false,
				dataIndex : 'rid',
				renderer : sys.rd.rid
			}, {
				header : 'Author',
				sortable : false,
				dataIndex : 'username',
				renderer : sys.rd.author
			}, {
				header : 'PID',
				sortable : false,
				dataIndex : 'pid',
				renderer : sys.rd.pid
			}, {
				header : 'Status',
				sortable : false,
				dataIndex : 'status',
				renderer : sys.rd.status,
				width : 130
			}, {
				header : 'Language',
				sortable : false,
				dataIndex : 'lang'
			}, {
				header : 'CodeLength',
				sortable : false,
				dataIndex : 'codeLen',
				renderer : sys.rd.codelen
			}, {
				header : 'ExeTime',
				sortable : false,
				dataIndex : 'exeTime',
				renderer : sys.rd.exetime
			}, {
				header : 'ExeMemory',
				sortable : false,
				dataIndex : 'exeMem',
				renderer : sys.rd.exemem
			}, {
				header : 'SubmitTime',
				sortable : false,
				dataIndex : 'submitTime',
				width : 130
			}]);

	var sm = new Ext.grid.RowSelectionModel({
				singleSelect : true
			});

	var toolbar = new Ext.Toolbar({
				items : [new Ext.Button({
									text : '刷新',
									iconCls : 'refresh-icon',
									handler : function() {
										store.reload();
									}
								}), new Ext.Toolbar.Separator()]
			});
	var onlymy = new Ext.Button({
				text : '只看我',
				iconCls : 'mysubmit-icon',
				enableToggle : true,
				pressed : false,
				toggleHandler : function(sender, pressed) {
					if (sys.guest)
						onlymy.toggle(false, true);
					else
						store.reloadParams({
									username : pressed ? sys.user.username : null,
									start : 0,
									limit : pageSize
								});
				}
			});
	var finds = new Ext.Button({
				text : '查找',
				iconCls : 'find-icon',
				enableToggle : true,
				pressed : false,
				toggleHandler : function(sender, pressed) {
					if (pressed) {
						findStatus();
						finds.toggle(false, true);
					} else {
						onlymy.toggle(false, true);
						store.load({
									params : {
										start : 0,
										limit : pageSize
									}
								});
					}
				}
			});
	toolbar.add([onlymy, finds]);
	if (sys.god) {
		godbuttons = [new Ext.Toolbar.Separator(), new Ext.Button({
							text : '重判',
							iconCls : 'rejudge-icon',
							handler : onRejudgeStatus,
							disabled : true
						}), new Ext.Button({
							text : '测试',
							iconCls : 'test-icon',
							handler : onTest,
							disabled : true
						}), new Ext.Button({
							text : '删除',
							iconCls : 'delete-icon',
							handler : onDelete,
							disabled : true
						})];
		toolbar.add(godbuttons);
	}

	var pagebar = new Ext.PagingToolbar({
				pageSize : pageSize,
				store : store,
				displayInfo : true,
				emptyMsg : '当前没有查询到状态。',
				displayMsg : '当前显示最新第 {0} - {1} 条状态，共 {2} 条状态。',
				plugins : new Ext.ux.SlidingPager()
			});

	var grid = new Ext.grid.GridPanel({
				id : 'showStatusList',
				title : 'OnlineStatus',
				iconCls : 'status-icon',
				closable : true,
				region : 'center',
				loadMask : {
					msg : '载入中...'
				},
				viewConfig : {
					forceFit : true
				},
				store : store,
				cm : cm,
				sm : sm,
				tbar : toolbar,
				bbar : pagebar
			});
	if (sys.god) sm.on('selectionchange', onSelect);
	grid.on('rowdblclick', ondblclick);
	grid.onRefresh = onRefresh;
	addTab(grid);

	if (sys.refreshstatuslisttask) sys.refreshstatuslisttask.stopAll();
	sys.refreshstatuslisttask = new Ext.util.TaskRunner();
	sys.refreshstatuslisttask.start({
				run : function() {
					if (!refreshTab('showStatusList', null, true)) return false;
				},
				interval : 20000
			});

	onRefresh(obj);
	function onRefresh(obj) {
		if (!obj) obj = {};
		var params = {
			limit : pageSize
		};
		if (Ext.isNumber(obj.page))
			params.start = pageSize * (obj.page - 1);
		else
			params.start = 0;
		var isme = false;
		var isfind = false;
		if (!Ext.isEmpty(obj.username)) {
			params.username = obj.username;
			if (sys.user
					&& Ext.util.Format.lowercase(sys.user.username) == Ext.util.Format
							.lowercase(obj.username))
				isme = true;
			else
				isfind = true;
		}
		if (!Ext.isEmpty(obj.pid)) {
			params.pid = obj.pid;
			isfind = true;
		}
		if (!Ext.isEmpty(obj.school)) {
			params.school = obj.school;
			isfind = true;
		}
		if (!Ext.isEmpty(obj.lang)) {
			params.lang = obj.lang;
			isfind = true;
		}
		if (!Ext.isEmpty(obj.status)) {
			params.status = obj.status;
			isfind = true;
		}
		if (!Ext.isNumber(obj.page) && !isme && !isfind) {
			store.reload();
			return;
		}
		onlymy.toggle(isme, true);
		finds.toggle(isfind, true);
		store.load({
					params : params
				});
	}
	function onRejudgeStatus() {
		var select = getSelect();
		if (!select) return;
		rejudgeStatus(select.data.rid);
	}
	function onTest() {
		var r = getSelect();
		if (!r) return;
		testStatus(r.data.rid);
	}
	function onDelete() {
		var select = getSelect();
		if (!select) return;
		deleteStatus(select.data.rid);
	}
	function onSelect() {
		var record = getSelect();
		var disable = !record;
		godbuttons[1].setDisabled(disable);
		godbuttons[2].setDisabled(disable);
		godbuttons[3].setDisabled(disable);
	}
	function getSelect() {
		if (sm.getCount() == 0) return null;
		return sm.getSelected();
	}
	function ondblclick() {
		if (sys.guest) return;
		var record = getSelect();
		if (!record) return;
		showStatus(record.data.rid);
	}
}

function deleteStatus(obj) {
	obj = defaultx(obj, 'rid');
	confirmx('您确定要删除状态 - ' + obj.rid + ' 吗？<br />对应的统计会一并更新。', function() {
				ajax({
							url : url.deleteStatus,
							params : {
								rid : obj.rid
							},
							success : function() {
								alertx('删除成功！');
								refreshTab('showStatusList');
								if (obj.success) obj.success();
							}
						});
			});
}

function rejudgeStatus(obj) {
	obj = defaultx(obj, 'rid');
	confirmx('您确定要重判状态 - ' + obj.rid + ' 吗？<br />对应的统计会一并更新。', function() {
				ajax({
							url : url.rejudgeStatus,
							params : {
								rid : obj.rid
							},
							success : function() {
								alertx('服务器接受了重判请求，正在重判...');
								refreshTab('showStatusList');
								if (obj.success) obj.success();
							}
						});
			});
}
